package com.greentreecs.fms.controller;

import com.greentreecs.fms.constant.ReportTableConstant;
import com.greentreecs.fms.pojo.dto.FeeRelevantTableDTO;
import com.greentreecs.fms.pojo.dto.IncOutcBalanceDTO;
import com.greentreecs.fms.pojo.response.GeneralResponse;
import com.greentreecs.fms.service.AccountingService;
import com.greentreecs.fms.util.ExportExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刀氏春秋
 * @date 2023/11/23
 */
@RequestMapping("/accounting")
@RestController
@Api(value = "住宿收支统计", tags = "住宿收支统计")
public class AccountingBalanceController {
    @Autowired
    private AccountingService accountingService;

    /**
     * 读取原始报表（3号、4号和6号表）
     *
     * @param time 报表时间
     * @return 报表数据组合
     */
    @ApiOperation(value = "原始报表读取", notes = "原始报表读取")
    @GetMapping(value = "/query-all-origin-table")
    public GeneralResponse<FeeRelevantTableDTO> queryAllOriginTable(String time) {
        GeneralResponse<FeeRelevantTableDTO> response = new GeneralResponse<>();
        // 获取原始报表查询结果
        response.fillResponseWith(accountingService.fetchAllOriginTable(time));
        return response;
    }

    /**
     * 核对收入支出平衡接口
     *
     * @param time 报表时间
     * @return 收入支出平衡明细表
     */
    @ApiOperation(value = "核对收入支出平衡", notes = "核对收入支出平衡")
    @GetMapping(value = "/inc-outc-balance")
    public GeneralResponse<List<IncOutcBalanceDTO>> checkIncOutcBalance(String time) {
        GeneralResponse<List<IncOutcBalanceDTO>> response = new GeneralResponse<>();
        // 获取收支计算结果并放入响应体
        response.fillResponseWith(accountingService.checkIncOutcBalance(time));
        if (response.getContent() != null) {
            // 如果结果不为空，代表计算成功
            // 使用导出工具将结果导出为excel表
            ExportExcelUtil<IncOutcBalanceDTO> exporter = new ExportExcelUtil<>();
            exporter.exportResultData(response.getContent(),
                    ReportTableConstant.DAY_INC_OUTC_BAL_TABLE, IncOutcBalanceDTO.class);
        }
        return response;
    }

    /**
     * 合并收入支出平衡明细到历史表
     *
     * @param toBeMergedData 待合并表
     * @return 历史表
     */
    @ApiOperation(value = "合并收入支出平衡明细到历史表", notes = "合并收入支出平衡明细到历史表")
    @PostMapping(value = "/merge-inc-outc-to-history")
    public GeneralResponse<List<IncOutcBalanceDTO>> mergeIncOutBalToHistory(@RequestBody List<IncOutcBalanceDTO> toBeMergedData) {
        GeneralResponse<List<IncOutcBalanceDTO>> response = new GeneralResponse<>();
        // 传入新平衡明细，与历史明细合并
        response.fillResponseWith(accountingService.mergeIncOutcBalToHistory(toBeMergedData));
        if (response.getContent() != null) {
            // 如果结果不为空，代表计算成功
            // 使用导出工具将结果导出为excel表
            ExportExcelUtil<IncOutcBalanceDTO> exporter = new ExportExcelUtil<>();
            exporter.exportResultDataWithoutDate(response.getContent(),
                    ReportTableConstant.HISTORY_INC_OUTC_BAL_TABLE, IncOutcBalanceDTO.class);
        }
        return response;
    }

    /**
     * 修改历史表（只导出即可）
     *
     * @param modifiedData 新历史表
     * @return 新历史表
     */
    @ApiOperation(value = "修改历史收入支出平衡明细表", notes = "修改历史收入支出平衡明细表")
    @PostMapping(value = "/modify-history")
    public GeneralResponse<List<IncOutcBalanceDTO>> modifyHistoryBalance(@RequestBody List<IncOutcBalanceDTO> modifiedData) {
        GeneralResponse<List<IncOutcBalanceDTO>> response = new GeneralResponse<>();
        response.fillResponseWith(accountingService.deleteHistoryRecBal(modifiedData));
        ExportExcelUtil<IncOutcBalanceDTO> exporter = new ExportExcelUtil<>();
        exporter.exportResultDataWithoutDate(response.getContent(),
                ReportTableConstant.HISTORY_INC_OUTC_BAL_TABLE, IncOutcBalanceDTO.class);
        return response;
    }
}
